Teacher Version

Hero Image
Hero Image

Commenting

Adding comments to a piece of code

Commenting a program means using descriptive text to explain portions of code. The compiler and robot both ignore comments when running the program, allowing a programmer to leave important notes in non-code format alongside the program code itself. This is considered a very good programming style because it cuts down on potential confusion later on, when someone else (or even you) may need to read the code.

To add a comment block in ROBOTC, click and drag the comment block into your code.

This is an example of a program with single line comments.

"Commenting Out" Code

Commenting is also sometimes used to temporarily "disable" code in a program without actually deleting it. In the program below, the programmer has code to move an arm up and then move the arm down. However, in order to test only the second half of the program, the programmer made the first behavior into a comment so the robot will ignore it. The programmer turned lines of code into a comment by clicking on the line number. When the programmer is done testing the second behavior, they can click on the // comment marks to re-enable the lines of code in the program.

This is a sample of code that has been "commented out" for testing only part of the code.

Good, Great, and Worse Commenting

Great commenting is similar to showing your work, providing insight into why the program is working or not. Good commenting could have a short description on the behavior. Little or no commenting is worse and could make your code difficult for someone to understand.

The image above shows an example of great commenting. Through commenting, many can understand how the robot gets to 50 cm.

The image above shows an example of good commenting.

The image above is an example code without comments.

© 2016 Robomatter, Inc.

Portions of this product are manufactured under license from Carnegie Mellon University.